<?php

/***************************************************************\
* FILE:		/sys/forumfunctions.inc
* SECTION:	core > directory listings
* FUNCTION:	looks for subfora, mainly used by .dir and .fora
* USES:		
* INCLUDES:	
* PRODUCES:	
* LAUNCHES:	
\***************************************************************/

function fora_page_inc_check_occurences() {
	global $PAGE;

	$THISDIR = $PAGE['directory_tree'][$PAGE['dir_depth'] - 1];

	$PAGE['occurences'] = array();

	$occarr = array();
	$dir = $THISDIR['dir_id'];
	$link = "";
	while (($dir != 0) and ($PAGE['menu'][$dir]['access'] != "DENY")) {
		array_unshift($occarr, array('name' => $PAGE['menu'][$dir]['menuname'], 'link' => $PAGE['menu'][$dir]['name']));
		$dir = $PAGE['menu'][$dir]['parent_id'];
	}
	if ($PAGE['menu'][$dir]['access'] != "DENY") {
		$occarr[] = array('link' => "{$occ['sub_id']}.subset");
		$PAGE['occurences'][] = $occarr;
	}
}

function fora_get_subfora($parent_id, $menu) {

	if (!is_array($menu[$parent_id]['children']) or !count($menu[$parent_id]['children']))
		return array();

	$fora = array();
	
	$sql = db_query_select(array(array(), "dir_id, COUNT(DISTINCT ITEM_IN.item_id) AS num_items, COUNT(post_id) AS num_posts, MAX(POST.edit) AS last_edit"), 
				array("(SUB_IN INNER JOIN ITEM_IN ON SUB_IN.sub_id = ITEM_IN.sub_id) LEFT JOIN POST ON ITEM_IN.item_id = POST.item_id", "DUMMY"), 
				"dir_id IN (" . implode(",", $menu[$parent_id]['children']) . ")", "", "", "dir_id");
	$sqlresult = db_do_query($sql);
	$info = array();
	while ($d = db_fetch_array($sqlresult)) {
		$info[$d['dir_id']] = $d;
	}
	reset($menu[$parent_id]['children']);
	do {
		$id = current($menu[$parent_id]['children']);
		$p = $id;
		while ($p > 0) {
			$info[$id]['url'] = "/" . $menu[$p]['name'] . $info[$id]['url'];
			$p = $menu[$p]['parent_id'];
		}
		if ($menu[$id]['access'] != "DENY") { // if INHERIT, parent must be allow to reach this stage
			$fora[] = array_merge($menu[$id], $info[$id]);
		}
	} while (next($menu[$parent_id]['children']));
	
	return $fora;
}

function fora_fetch_multiple($root, $menu, $arr) {

	if (!is_array($menu[$root]['children']))
		return array();

	reset($menu[$root]['children']);
	do {
		$id = current($menu[$root]['children']);
		if (is_array($menu[$id]['children']) and count($menu[$id]['children']))
			$arr[] = array('fora' => fora_get_subfora($id, $menu), 'title' => $menu[$id]['title']);
	} while (next($menu[$root]['children']));
	
	return $arr;
}

?>
